package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.cim.exception.PersistentException;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.generation.HydroGeneratingUnit;
import com.sduept.cim.model.generation.ThermalGeneratingUnit;
import com.sduept.cim.model.protection.ProtectionEquipment;
import com.sduept.cim.model.wires.*;
import com.sduept.cim.persist.service.impl.CimPersistManager;
import com.sduept.cim.query.service.ProtectionEquipmentQueryService;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.cim.BxCimManager;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.model.edata.cim.bx.BxSubstation;
import com.sduept.nwld.dataserver.util.CimUtils;
import com.sduept.nwld.wave.util.CfgChnsParserUtil;
import com.sduept.nwld.wave.util.ProtectionNameParseUtil;
import com.sduept.nwld.wave.util.ProtectionNwUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
public class ProtectEquipmentETLManager {

	@Autowired
	private CimManager cm;

	@Autowired
	private SuperDAO dao;

	@Autowired
	private ProtectionEquipmentQueryService peq;
	@Autowired
	private CimPersistManager cimPersistManager;

	@Autowired
	private ETLDAO eDao;

	@Autowired
	private BxCimManager bxManager;

	/**
	 * 增量解析更新cim保护数据
	 * 
	 * @param date 更新时间
	 */
	public void incrementalUpdating(String date) {
		List<?> bxList = getAllSourceData(date);
		incrementalUpdating(bxList);
	}

	public void updateEquipemnt() {
		List<ProtectionEquipment> list = peq.findDeviceByCondication(" protectedId is null or subId is null", null,
				ProtectionEquipment.class);
		SFProtectEquipmentSave sfProtectEquipmentSave = new SFProtectEquipmentSave();
		sfProtectEquipmentSave.updateBean(list);
		log.info("更新完成");
	}

	public List<?> getAllSourceData() {
		List<?> result = eDao.query(
				"select mRID,mProtectName,pt_id,mPrimdevType,StationID,mPrimdevID,mCommuStatus,mRunStatus,devManufacturer,devType,mStationID,mFdevID from t_bx_protection_equipment");
		return result;
	}

	public List<?> getAllSourceData(String date) {
		String sql = "select mRID,mProtectName,pt_id,mPrimdevType,StationID,mPrimdevID,mCommuStatus,mRunStatus,devManufacturer,devType,mStationID,mFdevID from t_bx_protection_equipment where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}

	/**
	 * 增量解析更新cim保护数据(以保信变电站的，变电站名称+保护名称作为唯一字段)
	 * 
	 * @param bxList 保信数据 select
	 *               mRID,mProtectName,pt_id,mPrimdevType,StationID,mPrimdevID,mCommuStatus,mRunStatus,devManufacturer,devType,mStationID,mFdevID
	 *               from t_bx_protection_equipment
	 */
	public void incrementalUpdating(List<?> bxList) {
		// 获取当前cim中所有的保护设备
		List<ProtectionEquipment> cimList = peq.listAll();
		CopyOnWriteArrayList<ProtectionEquipment> needDeletes = new CopyOnWriteArrayList<>();
		for (ProtectionEquipment pre : cimList) {
			boolean noExist = true;
			Iterator<?> iter = bxList.iterator();
			label: while (iter.hasNext()) {
				Object obj = iter.next();
				Object[] objs = (Object[]) obj;
				if (pre.getId().equals(objs[0].toString())) {// 如果保信的id再cim中存在
					// 保信name和cim name 一致，无改动并且cim数据中protected_id被保护设备id不为空时无需更新cim中数据，否则需要重新解析
					if (StringUtils.isNotBlank(pre.getName()) && pre.getName().equals(objs[11].toString())
							&& StringUtils.isNotBlank(pre.getProtectedId())) {
						iter.remove();
					}
					noExist = false;
					break label;
				}
			}
			if (noExist) {// 如果不存在，表示当前的保信系统已经没有这个设备
				needDeletes.add(pre);
			}
		}
		if (bxList.size() > 0) {// 由于解析方法较慢，故有数据需要解析时再解析
			log.info("增加保护设备：" + bxList.size());
			SFProtectEquipmentSave equipmentSave = new SFProtectEquipmentSave(bxList);
			equipmentSave.incrementalSave();// 增量解析保存
		}
		log.info("需要删除的保护设备：" + needDeletes.size());
	}

	public class SFProtectEquipmentSave {
		private List<?> list;
		Map<String, Substation> cimStationMap = new HashMap<String, Substation>();
		Map<String, List<ACLineSegment>> aclineMap = new HashMap<String, List<ACLineSegment>>();
		Map<String, List<BusbarSection>> busMap = new HashMap<String, List<BusbarSection>>();
		Map<String, List<Breaker>> breakMap = new HashMap<String, List<Breaker>>();
		Map<String, List<PowerTransformer>> powerkMap = new HashMap<String, List<PowerTransformer>>();
		Map<String, List<Compensator>> comkMap = new HashMap<String, List<Compensator>>();
		Map<String, List<HydroGeneratingUnit>> hydroMap = new HashMap<String, List<HydroGeneratingUnit>>();
		Map<String, List<ThermalGeneratingUnit>> therMap = new HashMap<String, List<ThermalGeneratingUnit>>();
		Map<String, BxSubstation> bxStationMap = new HashMap<String, BxSubstation>();
		Map<String, BxSubstation> bxStationNameMap = new HashMap<String, BxSubstation>();
		private List<String> ids = new ArrayList<String>();

		public SFProtectEquipmentSave(List<?> list) {
			super();
			this.list = list;
			initData();
		}

		public SFProtectEquipmentSave() {
			super();
			initData();
		}

		private List<Substation> allStations;
		private List<ACLineSegment> allAclines;
		private List<BusbarSection> allBusbarSections;
		private List<Breaker> allBreakers;
		private List<PowerTransformer> allPowerTransformers;
		private List<Compensator> allCompensators;
		private List<ThermalGeneratingUnit> allThermalGeneratingUnits;
		private List<HydroGeneratingUnit> allHydroGeneratingUnits;
		private List<BxSubstation> allBxStations;

		private void initData() {
			Thread thread = new Thread() {

				@Override
				public void run() {

					allStations = cm.getAllStations();
					allAclines = cm.getAllAclines();
				}
			};
			Thread thread2 = new Thread() {

				@Override
				public void run() {

					allBusbarSections = cm.getAllBusbarSections();
					allBreakers = cm.getAllBreakers();
				}
			};
			Thread thread3 = new Thread() {

				@Override
				public void run() {

					allPowerTransformers = cm.getAllPowerTransformers();
					allCompensators = cm.getAllCompensators();
				};
			};
			Thread thread4 = new Thread() {

				@Override
				public void run() {

					allThermalGeneratingUnits = cm.getAllThermalGeneratingUnits();
					allHydroGeneratingUnits = cm.getAllHydroGeneratingUnits();
				}
			};
			Thread thread5 = new Thread() {

				@Override
				public void run() {
					allBxStations = bxManager.listAllBxSubstations();
				}
			};
			thread.start();
			thread2.start();
			thread3.start();
			thread4.start();
			thread5.start();
			try {

				thread.join();// 线程都结束后再执行主方法
				thread2.join();
				thread3.join();
				thread4.join();
				thread5.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			for (BxSubstation substation : allBxStations) {
				if (!StringUtils.isEmpty(substation.getStationid())) {
					bxStationMap.put(substation.getStationid(), substation);
					bxStationNameMap.put(substation.getMname(), substation);
				}
			}
			for (Substation station : allStations) {
				cimStationMap.put(station.getId(), station);
				List<ACLineSegment> aclines = new ArrayList<ACLineSegment>();
				for (ACLineSegment acline : allAclines) {
					if (station.getId().equals(acline.getStartST()) || station.getId().equals(acline.getEndST())) {
						aclines.add(acline);
					}
				}
				aclineMap.put(station.getId(), aclines);
				List<BusbarSection> buses = new ArrayList<BusbarSection>();
				for (BusbarSection bus : allBusbarSections) {
					if (station.getId().equals(bus.getSubId())) {
						buses.add(bus);
					}
				}
				busMap.put(station.getId(), buses);
				List<Breaker> breakers = new ArrayList<Breaker>();
				for (Breaker breaker : allBreakers) {
					if (station.getId().equals(breaker.getSubId())) {
						breakers.add(breaker);
					}
				}
				breakMap.put(station.getId(), breakers);

				List<PowerTransformer> powers = new ArrayList<PowerTransformer>();
				for (PowerTransformer power : allPowerTransformers) {
					if (station.getId().equals(power.getSubId())) {
						powers.add(power);
					}
				}
				powerkMap.put(station.getId(), powers);

				List<Compensator> coms = new ArrayList<Compensator>();
				for (Compensator com : allCompensators) {
					if (station.getId().equals(com.getSubId())) {
						coms.add(com);
					}
				}
				comkMap.put(station.getId(), coms);
				List<HydroGeneratingUnit> hunits = new ArrayList<HydroGeneratingUnit>();
				for (HydroGeneratingUnit hunit : allHydroGeneratingUnits) {
					if (station.getId().equals(hunit.getSubId())) {
						hunits.add(hunit);
					}
				}
				hydroMap.put(station.getId(), hunits);
				List<ThermalGeneratingUnit> units = new ArrayList<ThermalGeneratingUnit>();
				for (ThermalGeneratingUnit unit : allThermalGeneratingUnits) {
					if (station.getId().equals(unit.getSubId())) {
						units.add(unit);
					}
				}
				therMap.put(station.getId(), units);

			}
			String sql = "select distinct id from t_cim_protection_equipment ";
			List<?> nativeQuery = dao.nativeQuery(sql);
			ids = (List<String>) nativeQuery;

		}

		/**
		 * 更新保护设备
		 * 
		 * @param equipments
		 */
		public void updateBean(List<ProtectionEquipment> equipments) {
			for (ProtectionEquipment equipment : equipments) {
				if (StringUtils.isEmpty(equipment.getSubId())) {
					// 更新保护设备的变电站信息
					BxSubstation substation = bxStationMap.get(equipment.getPrivateStationId());
					if (substation != null) {
						Substation stationId = cimStationMap.get(substation.getCimId());// 变电站
						if (stationId != null) {
							equipment.setSubId(stationId.getId());
							equipment.setSubName(stationId.getName());
							equipment.setStandvoltage((int) stationId.getStandVoltagelevel());

						}
					}
				}
				if (StringUtils.isEmpty(equipment.getProtectedId())) {
					// 更新被保护设备信息
					if (StringUtils.isEmpty(equipment.getSubId())) {
						continue;
					}
					String[] s = getProtectedByNameAndType(equipment.getName(), equipment.getProtectedType(),
							equipment.getSubId());
					if (s != null) {
						equipment.setProtectedId(s[0]);
						equipment.setProtectedName(s[1]);
					}
				}
				try {
					cimPersistManager.update(equipment);
				} catch (PersistentException e) {
					e.printStackTrace();
				}
			}
		}

		/**
		 * 增量解析更新
		 */
		public void incrementalSave() {

			List<ProtectionEquipment> preList = new ArrayList<ProtectionEquipment>();
			for (Object obj : list) {
				Object[] objs = (Object[]) obj;
				for (int i = 0; i < objs.length; i++) {
					if (objs[i] != null && "null".equals(objs[i].toString())) {
						objs[i] = null;
					}
				}
				ProtectionEquipment equipment = new ProtectionEquipment();
				if (objs[0] != null) {
					equipment.setId(objs[0].toString());
				}
				String protectedType = "";// 一次设备类型
				if (objs[11] != null) {

					equipment.setName(objs[11].toString());
					protectedType = ProtectionNameParseUtil.getEquipmentType(objs[11].toString());// 根据name解析一次设备类型
					String proType = ProtectionNwUtil.getProtectionType(objs[11].toString());
					equipment.setType(getProTypeName(proType));
				}
				if (objs[10] != null)
					equipment.setPrivateStationName(objs[10].toString());
				if (objs[2] != null)
					equipment.setPtId(objs[2].toString());
				if (objs[6] != null)
					equipment.setmCommuStatus(Integer.parseInt(objs[6].toString()));
				if ((StringUtils.isEmpty(protectedType) || "未知".equals(protectedType)) && objs[3] != null) {// 解析型号为空或者未知时，以保信为准
					// 取到的保护类型为string
					equipment.setProtectedType(getStringType(objs[3].toString()));
				} else {
					equipment.setProtectedType(protectedType);
				}
				if (objs[7] != null)
					equipment.setmRunStatus(Integer.parseInt(objs[7].toString()));
				if (objs[9] != null) {
					equipment.setDevType(objs[9].toString());
				} else {
					// 如果没有保护型号，则根据名称抽取，兼容SF目前没有保护设备型号的情况
					String devType = getDevType(objs[11] == null ? "" : objs[11].toString());
					if (StringUtils.isEmpty(devType)) {
						devType = getSecondDevType(objs[11].toString());
					}
					if (StringUtils.isEmpty(devType)) {
						devType = getThirdDevType(objs[11].toString());
					}
					equipment.setDevType(devType);
				}
				if (objs[4] != null) {
					equipment.setPrivateStationId(objs[4].toString());
				} else {
					if (objs[10] != null) {
						BxSubstation bxSubstation = bxStationNameMap.get(objs[10].toString());
						if (bxSubstation != null) {
							equipment.setPrivateStationId(bxSubstation.getStationid());
						}
					}
				}
				if (objs[8] != null)
					equipment.setDevManufacturer(objs[8].toString());

				// 兼容mStationId 为空的情况
				Substation cimstation = null;
				if (equipment.getPrivateStationId() != null) {
					BxSubstation substation = bxStationMap.get(equipment.getPrivateStationId());
					if (substation != null) {
						cimstation = cimStationMap.get(substation.getCimId());// 变电站
					}
				} else if (equipment.getPrivateStationName() != null) {
					for (Substation s : allStations) {
						if (CimUtils.stationNameMatching(s.getName(), equipment.getPrivateStationName())) {
							cimstation = s;
							break;
						}
					}
				}
				if (cimstation != null) {
					equipment.setSubId(cimstation.getId());
					equipment.setSubName(cimstation.getName());
					equipment.setStandvoltage((int) cimstation.getStandVoltagelevel());
					String[] s = getProtectedByNameAndType(equipment.getName(), equipment.getProtectedType(),
							cimstation.getId());
					if (s != null) {
						equipment.setProtectedId(s[0]);
						equipment.setProtectedName(s[1]);
					}
				}
				preList.add(equipment);
			}
			try {
			cimPersistManager.batchUpdate(preList);
			log.info("更新保护设备：" + preList.size());
			} catch (PersistentException e) {
				e.printStackTrace();
			}
		}

		private String[] getProtectedByNameAndType(String name, String type, String stationId) {
			String[] s = new String[2];
			if ("母线".equals(type)) {
				List<BusbarSection> list = busMap.get(stationId);
				if (list != null && list.size() > 0) {
					for (BusbarSection bus : list) {
						String proName = CimUtils.getBusNameByProName(name);
						String busName = CimUtils.getBusNameByProName(bus.getName());
						if (CimUtils.busNameMatching(busName, proName)) {
							s[0] = bus.getId();
							s[1] = bus.getName();
							break;
						}
					}
				}
			} else if ("线路".equals(type)) {
				List<ACLineSegment> list = aclineMap.get(stationId);
				if (list != null && list.size() > 0) {
					for (ACLineSegment acline : list) {
						String proName = CfgChnsParserUtil.getLineNameOfNw(name);
						if (CimUtils.lineNameMatching(proName, acline.getName(), false)) {
							s[0] = acline.getId();
							s[1] = acline.getName();
							break;
						}
					}
				}
			} else if ("断路器".equals(type)) {
				List<Breaker> list = breakMap.get(stationId);
				if (list != null && list.size() > 0) {
					for (Breaker breaker : list) {
						String proName = CfgChnsParserUtil.getBreakerNameOfNw(name);
						if (CimUtils.breakerNameMatching(proName, breaker.getName())) {
							s[0] = breaker.getId();
							s[1] = breaker.getName();
							break;
						}
					}
				}
			} else if ("变压器".equals(type)) {
				List<PowerTransformer> list = powerkMap.get(stationId);
				if (list != null && list.size() > 0) {
					for (PowerTransformer power : list) {
						String proName = CfgChnsParserUtil.getPowerNameOfNw(name);
						String powerName = CfgChnsParserUtil.getPowerNameOfNw(power.getName());
						boolean eq = CimUtils.powerNameMatching(proName, powerName);
						if (eq) {
							s[0] = power.getId();
							s[1] = power.getName();
							break;
						}
					}
				}
			} else if ("电抗器".equals(type) || "电容器".equals(type)) {
				List<Compensator> list = comkMap.get(stationId);
				if (list != null && list.size() > 0) {
					for (Compensator com : list) {
						String proName = CfgChnsParserUtil.getCompenName(name);
						String comName = CfgChnsParserUtil.getCompenName(com.getName());
						boolean eq = CimUtils.compenNameMatching(proName, comName);
						if (eq) {
							s[0] = com.getId();
							s[1] = com.getName();
							break;
						}
					}
				}
			} else if ("发电机".equals(type)) {
				List<HydroGeneratingUnit> list = hydroMap.get(stationId);
				if (list != null && list.size() > 0) {
					for (HydroGeneratingUnit unit : list) {
						String proName = CimUtils.getUnitStringByProName(name);
						boolean eq = CimUtils.hyDroNameMatching(proName, unit.getName());
						if (eq) {
							s[0] = unit.getId();
							s[1] = unit.getName();
							return s;
						}
					}
				}
				List<ThermalGeneratingUnit> list1 = therMap.get(stationId);
				if (list != null && list.size() > 0) {
					for (ThermalGeneratingUnit unit : list1) {
						String proName = CimUtils.getUnitStringByProName(name);
						boolean eq = CimUtils.hyDroNameMatching(proName, unit.getName());
						if (eq) {
							s[0] = unit.getId();
							s[1] = unit.getName();
							return s;
						}
					}
				}
			} else {
				s[0] = null;
				s[1] = null;
			}

			return s;
		}

		public String getProTypeName(String type) {
			if ("PRIMARY_PROTECTION_1".equals(type)) {
				return "主一保护";
			} else if ("PRIMARY_PROTECTION_2".equals(type)) {
				return "主二保护";
			} else if ("PRIMARY_PROTECTION_3".equals(type)) {
				return "主三保护";
			} else if ("ASSIST_PROTECTION_1".equals(type)) {
				return "辅A保护";
			} else if ("ASSIST_PROTECTION_2".equals(type)) {
				return "辅B保护";
			} else if ("BREAKER_PROTECTION_1".equals(type)) {
				return "断路器保护";
			} else if ("NOELECTRONIC_PROTECTION".equals(type)) {
				return "非电量保护";
			} else if ("STUB_PROTECTION_1".equals(type)) {
				return "短引线保护一";
			} else if ("STUB_PROTECTION_2".equals(type)) {
				return "短引线保护二";
			} else if ("STUB_PROTECTION_3".equals(type)) {
				return "短引线保护三";
			} else if ("REACTOR_PROTECTION_1".equals(type)) {
				return "电抗器保护一";
			} else if ("REACTOR_PROTECTION_2".equals(type)) {
				return "电抗器保护二";
			} else if ("REACTOR_PROTECTION_3".equals(type)) {
				return "电抗器保护三";
			} else {
				return null;
			}
		}

		public Map<String, String> getDevManufacturer() {

			Map<String, String> mftMap = new HashMap<String, String>();// 保护厂家
			mftMap.put("RED", "ABB公司");
			mftMap.put("SGR", "ABB公司");
			mftMap.put("SBH", "上海置信");
			mftMap.put("CSC", "北京四方");
			mftMap.put("CSD", "北京四方");
			mftMap.put("BP_2B", "南瑞科技");
			mftMap.put("BP-2B", "南瑞科技");
			mftMap.put("BP2B", "南瑞科技");
			mftMap.put("BP-2CNWA", "南瑞科技");
			mftMap.put("RCS", "南瑞继保");
			mftMap.put("PCS", "南瑞继保");
			mftMap.put("DGT", "国电南自");
			mftMap.put("SSR", "国电南自");
			mftMap.put("WBZ", "国电南自");
			mftMap.put("WMZ", "国电南自");
			mftMap.put("PST", "国电南自");
			mftMap.put("PSL", "国电南自");
			mftMap.put("PSC", "国电南自");
			mftMap.put("SGB", "国电南自");
			mftMap.put("SG", "国电南自");
			mftMap.put("SG-R", "国电南自");
			mftMap.put("WDK", "国电南自");
			mftMap.put("WDGL", "山大电力");
			mftMap.put("WFB", "许继集团");
			mftMap.put("WBH", "许继集团");
			mftMap.put("SDR", "许继集团");
			mftMap.put("WDLK", "许继集团");
			mftMap.put("WXH", "许继集团");
			mftMap.put("WGQ", "许继集团");
			mftMap.put("WGB", "许继集团");
			mftMap.put("WKB", "许继集团");
			mftMap.put("HCM", "许继集团");
			mftMap.put("PRS", "长园深瑞");
			mftMap.put("BP-2C", "长园深瑞");
			mftMap.put("BP2C", "长园深瑞");
			mftMap.put("GE", "上海北科");

			return mftMap;
		}

		public List<?> getList() {
			return list;
		}

		public void setList(List<?> list) {
			this.list = list;
		}

	}

	private String getStringType(String type) {
		String strType = null;
		switch (type) {
		case "未定义1":
			strType = "未定义1";
			break;
		case "母差保护":
			strType = "母线";
			break;
		case "变压器保护":
			strType = "变压器";
			break;
		case "线路保护":
			strType = "线路";
			break;
		case "发变组保护":
			strType = "发电机";
			break;
		case "断路器保护":
			strType = "断路器";
			break;
		case "电容器保护":
			strType = "电抗器";
			break;
		case "其他":
			strType = "其他";
			break;
		default:
			strType = "未知";
			break;
		}
		return strType;
	}

	public String getProTypeName(String type) {
		if ("PRIMARY_PROTECTION_1".equals(type)) {
			return "主一保护";
		} else if ("PRIMARY_PROTECTION_2".equals(type)) {
			return "主二保护";
		} else if ("PRIMARY_PROTECTION_3".equals(type)) {
			return "主三保护";
		} else if ("ASSIST_PROTECTION_1".equals(type)) {
			return "辅A保护";
		} else if ("ASSIST_PROTECTION_2".equals(type)) {
			return "辅B保护";
		} else if ("NOELECTRONIC_PROTECTION".equals(type)) {
			return "非电量保护";
		} else if ("STUB_PROTECTION_1".equals(type)) {
			return "短引线保护一";
		} else if ("STUB_PROTECTION_2".equals(type)) {
			return "短引线保护二";
		} else if ("STUB_PROTECTION_3".equals(type)) {
			return "短引线保护三";
		} else if ("REACTOR_PROTECTION_1".equals(type)) {
			return "电抗器保护一";
		} else if ("REACTOR_PROTECTION_2".equals(type)) {
			return "电抗器保护二";
		} else if ("REACTOR_PROTECTION_3".equals(type)) {
			return "电抗器保护三";
		} else if ("ELECTRONIC_PROTECTION".equals(type)) {
			return "电气量保护";
		} else if ("series_compensation_PROTECTION".equals(type)) {
			return "串补保护";
		} else {
			return null;
		}
	}

	public Map<String, String> getDevManufacturer() {

		Map<String, String> mftMap = new HashMap<String, String>();// 保护厂家
		mftMap.put("RED", "ABB公司");
		mftMap.put("SGR", "ABB公司");
		mftMap.put("SBH", "上海置信");
		mftMap.put("CSC", "北京四方");
		mftMap.put("CSD", "北京四方");
		mftMap.put("BP_2B", "南瑞科技");
		mftMap.put("BP-2B", "南瑞科技");
		mftMap.put("BP2B", "南瑞科技");
		mftMap.put("BP-2CNWA", "南瑞科技");
		mftMap.put("RCS", "南瑞继保");
		mftMap.put("PCS", "南瑞继保");
		mftMap.put("DGT", "国电南自");
		mftMap.put("SSR", "国电南自");
		mftMap.put("WBZ", "国电南自");
		mftMap.put("WMZ", "国电南自");
		mftMap.put("PST", "国电南自");
		mftMap.put("PSL", "国电南自");
		mftMap.put("PSC", "国电南自");
		mftMap.put("SGB", "国电南自");
		mftMap.put("SG", "国电南自");
		mftMap.put("SG-R", "国电南自");
		mftMap.put("WDK", "国电南自");
		mftMap.put("WDGL", "山大电力");
		mftMap.put("WFB", "许继集团");
		mftMap.put("WBH", "许继集团");
		mftMap.put("SDR", "许继集团");
		mftMap.put("WDLK", "许继集团");
		mftMap.put("WXH", "许继集团");
		mftMap.put("WGQ", "许继集团");
		mftMap.put("WGB", "许继集团");
		mftMap.put("WKB", "许继集团");
		mftMap.put("HCM", "许继集团");
		mftMap.put("PRS", "长园深瑞");
		mftMap.put("BP-2C", "长园深瑞");
		mftMap.put("BP2C", "长园深瑞");
		mftMap.put("GE", "上海北科");

		return mftMap;
	}

	public String getDevType(String name) {
		String s = null;
		name = name.replaceAll("[0-9]{1,3}kV", "");
		String regex = "[A-Za-z]{2,4}[0-9_-]+[A-Z0-9_-]*";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(name);
		while (matcher.find()) {
			s = matcher.group(0);
			break;
		}
		return s;
	}

	public String getSecondDevType(String name) {
		String s = null;
//		name=name.replace(" ", "");
		String regex = "[A-Za-z]{2,4}[0-9_－]+[A-Z0-9_-]*";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(name);
		while (matcher.find()) {
			s = matcher.group(0);
			break;
		}
		return s;
	}

	public String getThirdDevType(String name) {
		String s = null;
//		name=name.replace(" ", "");
		String regex = "[A-Za-z]{1,4}[0-9_-]+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(name);
		while (matcher.find()) {
			s = matcher.group(0);
			break;
		}
		return s;
	}

	private String getKey(String name) {
		String s = null;
		String regex = "[A-Z]{2,}";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(name);
		while (matcher.find()) {
			s = matcher.group(0);
			break;
		}
		return s;
	}

	private String getStringTypeByMPrimdevID(String mPrimdevID) {
		if (mPrimdevID.contains("XL")) {// XL 线路
			return "线路";
		} else if (mPrimdevID.contains("MX")) {// MX 母线
			return "母线";
		} else if (mPrimdevID.contains("KG")) {// KG 断路器
			return "断路器";
		} else if (mPrimdevID.contains("FDJ")) {// FDJ 发电机
			return "发电机";
		} else if (mPrimdevID.contains("BYQ")) {// BYQ 变压器
			return "变压器";
		} else if (mPrimdevID.contains("DRQ")) {// DRQ 电容器
			return "电容器";
		} else if (mPrimdevID.contains("DKQ")) {// DKQ 电抗器
			return "电抗器";
		} else {
			return "未知";
		}
	}

}
